package com.example.demo.yzy.controller;

import com.example.demo.exception.ExceptionEnum;
import com.example.demo.exception.ResultCode;
import com.example.demo.exception.throwtype.RunException;
import com.example.demo.yzy.dao.Person;
import com.example.demo.yzy.server.TestServer;
import com.example.demo.yzy.server.impl.TestSeverImpl;
import com.example.demo.yzy.vo.QueryVo;
import com.example.demo.yzy.vo.TestVo;
import com.example.demo.yzy.vo.UpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @description: TODO
* @author 杨镇宇
* @date 2022/3/24 14:58
* @version 1.0
*/
@Api(value = "练习", tags = {" 练习"})
@RestController
@Validated
@RequestMapping(value="api/test")
public class TestCotroller {
    @Autowired
    private TestServer server;
    private static Logger log = LoggerFactory.getLogger(TestSeverImpl.class);

    @ApiOperation(value = "新增")
    @CrossOrigin(origins = "*")
    @PostMapping("/insert")
    public int insert(@Validated TestVo vo) throws NoSuchFieldException, IllegalAccessException {
       return server.insert(vo);
    }
    @ApiOperation(value = "修改 根据name修改")
    @CrossOrigin(origins = "*")
    @PostMapping("/update")
    public int update(@Validated UpdateVo vo){
        String msg = checkNull(new String[]{"name#姓名#0"},
                vo
        );
        if (!"".equals(msg)){
            ResultCode r = ExceptionEnum.INVALID_PARAM;
            r.setDesc(msg);
            throw  new RunException(r);
        }
        return server.update(vo);
    }

    @ApiOperation(value = "修改 根据主键修改")
    @CrossOrigin(origins = "*")
    @PostMapping("/updateById")
    public int updateById(UpdateVo vo){
        String msg = checkNull(new String[]{"id#ID#5"},
                vo
        );
        if (!"".equals(msg)){
            ResultCode r = ExceptionEnum.INVALID_PARAM;
            r.setDesc(msg);
            throw  new RunException(r);
        }
        return server.updateById(vo);
    }

    @ApiOperation(value = "根据查询条件查询")
    @CrossOrigin(origins = "*")
    @PostMapping("/select")
    public List<Person> select(@Validated QueryVo vo){
        return server.select(vo);
    }

    @ApiOperation(value = "删除")
    @CrossOrigin(origins = "*")
    @PostMapping("/delete")
    public int delete( @Validated UpdateVo vo){
        String msg = checkNull(new String[]{"id#ID#5"},
                vo
        );
        if (!"".equals(msg)){
            ResultCode r = ExceptionEnum.INVALID_PARAM;
            r.setDesc(msg);
            throw  new RunException(r);
        }
        return server.delete(vo);
    }

    @ApiOperation(value = "根据name删除")
    @CrossOrigin(origins = "*")
    @PostMapping("/deleteByName")
    public int deleteByName(@Validated UpdateVo vo){
        String msg = checkNull(new String[]{"name#姓名#0"},
                vo
        );
        if (!"".equals(msg)){
            ResultCode r = ExceptionEnum.INVALID_PARAM;
            r.setDesc(msg);
            throw  new RunException(r);
        }
        return server.deleteByName(vo);
    }

    public static String checkNull(String [] inputs,UpdateVo vo){
        try{
            Class<? extends UpdateVo> clazz = vo.getClass();
            for (String input:inputs){
                String[] data = input.split("#");
                char [] get = data[0].toCharArray();
                get[0]-=32;
                String msg = data[1];
                switch (data[2]){
                    //0:String、1:Integer、2:BigDecimal、3:Short、4:Date、5:Long
                    case "0":
                        //String
                        String invoke =(String)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke == null || "".equals(invoke)){
                            return msg+"不能为空";
                        }
                        break;
                    case "1":
                        //Integer
                        Integer invoke1 =(Integer)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke1 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    case "2":
                        //BigDecimal
                        BigDecimal invoke2 =(BigDecimal)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke2 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    case "3":
                        //Short
                        Short invoke3 =(Short)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke3 == null){
                            return msg+"不能为空";
                        }
                        break;
                    case "4":
                        //Date
                        Date invoke4 =(Date)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke4 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    case "5":
                        //Long
                        Long invoke5 =(Long)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke5 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    default:
                        return "校验类型输入错误";
                }
            }
            return "";
        }catch (Exception e1){
            e1.printStackTrace();
            return "";
        }

    }

}
